package org.mineground.commands.irc;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.jibble.pircbot.Colors;
import org.jibble.pircbot.User;
import org.mineground.Main;
import org.mineground.Utilities;
import org.mineground.handlers.irc.CommandExecutor;
import org.mineground.handlers.irc.UserLevel;
import org.mineground.player.MinegroundPlayer;

/**
 * @name Unmute.java
 * @author Daniel Koenen (2012)
 */
public class Unmute implements CommandExecutor {
    @Override
    public void onCommand(User sender, UserLevel level, String channel, String command, String[] arguments) {
        if (level.compareTo(UserLevel.IRC_OP) < 0) {
	    return;
        }
        
        if (arguments.length == 0) {
            Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Usage:" + Colors.NORMAL + " !unmute [player]");
	    return;
	}
        
        Player mutePlayer = Main.getInstance().getServer().getPlayer(arguments[0]);
        
        if (mutePlayer == null) {
            Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Error: Invalid player.");
	    return;
	}
        
        MinegroundPlayer playerInstance = Main.getInstance().getPlayer(mutePlayer);
        
        if (playerInstance.isMuted() == MinegroundPlayer.MUTED_NOTMUTED) {
            Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Error: That player isn't muted.");
            return;
        }
        
        playerInstance.unmutePlayer();
        
        Main.getInstance().getIRCHandler().sendMessage(channel, Colors.DARK_GREEN + "* You have unmuted " + Utilities.fixName(mutePlayer) + Colors.DARK_GREEN + ".");
        mutePlayer.sendMessage(ChatColor.DARK_GREEN + "* You have been unmuted.");
    }
}